package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

import models.Objetivo;
import models.ObjetivoAluno;


public class DAOObjetivoAluno {

	public Set<ObjetivoAluno> select() throws Exception {
		Set<ObjetivoAluno> objs = new HashSet<ObjetivoAluno>();
		try {

			String query = "SELECT oa.idObjetivo, oa.idUsuario, o.nome, a.nome " +
					"FROM ObjetivoAluno oa, Usuario a, Objetivo o " +
					"WHERE oa.idObjetivo = o.idObjetivo " +
					"	AND oa.idUsuario = a.idUsuario";

			Statement statement = DAOConnection.getConnection().createStatement();

			ResultSet resultSet = statement.executeQuery(query);

			while(resultSet.next()) {
				ObjetivoAluno obj = new ObjetivoAluno();
				obj.idObjetivo = resultSet.getInt(1);
				obj.idUsuario = resultSet.getInt(2);
				obj.usuario.nome = resultSet.getString(3);
				obj.objetivo.nome = resultSet.getString(4);
				objs.add(obj);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return objs;
	}

	public Objetivo select(int id) throws Exception {
		Objetivo obj = null;
		try     {

			String query = "select idObjetivo, nome from Objetivo where idObjetivo = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
			statement.setInt(1, id);
			ResultSet resultSet = statement.executeQuery();

			while(resultSet.next()) {
				obj = new Objetivo();
				obj.idObjetivo = resultSet.getInt(1);
				obj.nome = resultSet.getString(2);

			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return obj;
	}

	public void insert(ObjetivoAluno oa) throws Exception {
		try {
			String insert = "insert into ObjetivoAluno (idUsuario, idObjetivo) values(?, ?)";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(insert);

			statement.setInt(1, oa.idUsuario);
			statement.setInt(2, oa.idObjetivo);


			statement.executeUpdate();
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void update(ObjetivoAluno oa) throws Exception {
		try {

			String update = "update ObjetivoAluno set idObjetivo = ?, idUsuario = ?  where idObjetivo = ? AND idUsuario = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(update);

			statement.setInt(1, oa.idObjetivo);
			statement.setInt(2, oa.idUsuario);

			statement.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void delete(ObjetivoAluno f) throws Exception {
		try {

			String delete = "DELETE FROM ObjetivoAluno WHERE idObjetivo = ? AND idUsuario = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(delete);

			statement.setInt(1, f.idObjetivo);
			statement.setInt(2, f.idUsuario);

			statement.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}

}